package cn.jit.util;

import java.util.Properties;

import cn.jit.support.RedisProperties;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {
	//地址
	private static String ADDR = "127.0.0.1";
	//Redis的端口号
	private static int PORT = 6379;
	//密码
	private static String AUTH = "root";
	//最大激活数目
	private static int MAX_ACTIVE = 1024;
	//最大空闲数目
	private static int MAX_IDLE = 200;
	//最大连接等待的时间 (单位:毫秒)
	private static int MAX_WAIT = 10000;
	//超时时间
	private static int TIMEOUT = 10000;
	//在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
	private static boolean TEST_ON_BORROW = true;
	
	private static JedisPool jedisPool = null;
	
	/**
	 * 初始化redis连接池	
	 */
	static {
        try {
        	Properties jedisProps = RedisProperties.getProperties();
			jedisProps.load(RedisUtil.class.getClassLoader().getResourceAsStream("redis.properties"));
			ADDR = jedisProps.getProperty("redis.addr");
			PORT = Integer.parseInt(jedisProps.getProperty("redis.port"));
			AUTH = jedisProps.getProperty("redis.auth");
			MAX_ACTIVE = Integer.parseInt(jedisProps.getProperty("redis.pool.maxActive"));
			MAX_IDLE = Integer.parseInt(jedisProps.getProperty("redis.pool.maxIdle"));
			MAX_WAIT = Integer.parseInt(jedisProps.getProperty("redis.pool.maxWait"));
			TIMEOUT = Integer.parseInt(jedisProps.getProperty("redis.pool.timeout"));
			TEST_ON_BORROW = Boolean.parseBoolean(jedisProps.getProperty("redis.pool.testOnBorrow"));
 
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setMaxIdle(MAX_IDLE);
            config.setTestOnBorrow(TEST_ON_BORROW);
            jedisPool = new JedisPool(config, ADDR, PORT, 3000, AUTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	public synchronized static Jedis getJedis() {
		try {
			if (jedisPool != null) {
				Jedis resource = jedisPool.getResource();
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void returnResource(final Jedis jedis) {
		if (jedis != null) {
			jedisPool.returnResource(jedis);
		}
	}
	
	/**
	 * 
	 * @param key
	 * @return 如果失败,返回null
	 */
	public static String get(String key) {
		String res = null;
		Jedis jedis = getJedis();
		res = jedis.get(key);
		returnResource(jedis);
		return res;
	}
	
	/**
	 * 设置redis
	 * @param key
	 * @param value
	 * @return 如果成功,返回OK
	 */
	public static String set(String key, String value) {
		String res = null;
		Jedis jedis = getJedis();
		res = jedis.set(key, value);
		returnResource(jedis);
		return res;
	}
	
	/**
	 * 1: the timeout was set. 
	 * 0: the timeout was not set since the key already has an associated timeout 
	 * @param key
	 * @param seconds
	 * @return
	 */
	public static long expire(String key, int seconds){
		Jedis jedis = getJedis();
		long res = jedis.expire(key, seconds);
		returnResource(jedis);
		return res;
	}
	
}
